import { NextRequest, NextResponse } from 'next/server'
import { requireAuth, withErrorHandling } from '@/lib/auth-middleware'
import { getUserSessionList } from '@/lib/auth-service'

export const GET = withErrorHandling(async (request: NextRequest) => {
  return requireAuth(request, async (req) => {
    try {
      const sessions = await getUserSessionList(req.user!.id)
      
      // 标记当前会话
      const token = request.headers.get('authorization')?.replace('Bearer ', '') || ''
      const updatedSessions = sessions.map(session => ({
        ...session,
        isCurrent: session.token === token
      }))
      
      return NextResponse.json({
        success: true,
        data: updatedSessions,
        message: '会话列表获取成功'
      })
    } catch (error) {
      console.error('获取会话列表失败:', error)
      return NextResponse.json({
        success: false,
        error: '获取会话列表失败'
      }, { status: 500 })
    }
  })
})